package com.campusflea.person;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.campusflea.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/17.
 */

public class DemoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private LayoutInflater mLayoutInfater;
    private List<DataModel> mlist=new ArrayList<>();
    public Context mcontent;
    public DemoAdapter(Context context) {
        mcontent=context;
        mLayoutInfater=LayoutInflater.from(context);
    }
    public void addList(List<DataModel> list){
        mlist.addAll(list);
    }

    public interface OnItemClickListener
    {
        void onItemClick(View view,int position);
        void onItemLongClick(View view,int position);
    }
    private OnItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(OnItemClickListener listener)
    {
        this.mOnItemClickListener=listener;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
//            case DataModel.TYPE_ONE:
//                return new TypeOneViewHolder(mLayoutInfater.inflate(R.layout.item_type_one,parent,false));
            case DataModel.TYPE_TWO:
                return new TypeTwoViewHolder(mLayoutInfater.inflate(R.layout.item_type_two,parent,false));
//            case DataModel.TYPE_THREE:
//                return new TypeThreeViewHolder(mLayoutInfater.inflate(R.layout.item_type_three,parent,false));

        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        int viewType=getItemViewType(position);
        ((TypeAbstractViewHolder)holder).binHolder(mlist.get(position));

        Picasso.with(mcontent).load(mlist.get(position).url).resize(90,90).into(((TypeAbstractViewHolder) holder).avatar);
        if(mOnItemClickListener!=null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    mOnItemClickListener.onItemClick(holder.itemView,position);

                }
            });
            //longclick
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onItemLongClick(holder.itemView,position);

                    return false;
                }
            });
        }

    }

    @Override
    public int getItemViewType(int position) {

        return mlist.get(position).type;
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }
}
